package edu.zqu.instructionalMatters.project.controller;

import edu.zqu.common.result.ZQUResult;
import edu.zqu.common.status.HttpStatus;
import edu.zqu.instructionalMatters.project.VO.CourseChangesApplicationStatusVO;
import edu.zqu.instructionalMatters.project.entity.CourseChangesApplicationStatus;
import edu.zqu.instructionalMatters.project.mapper.CourseChangesApplicationStatusMapper;
import edu.zqu.instructionalMatters.project.service.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 何文东
 * @Date 2020-07-03
 * @Description: 课程变动申请类型 - 前端控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/api")
@Api(tags = "课程变动申请类型字典")
public class CourseChangesApplicationStatusController {
    @Autowired
    private CourseChangesApplicationStatusMapper courseChangesApplicationStatusMapper;
    @Autowired
    private CollegeService collegeService;

    @ApiOperation(value = "获取课程变动申请类型字典列表")
    @RequestMapping(value = "/courseChangesApplicationStatus", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称关键字", paramType = "query", required = false),
            @ApiImplicitParam(name = "englishName", value = "英文名关键字", paramType = "query", required = false)
    })
    public ZQUResult getList(BigInteger collegeId,
                             String name,
                             String englishName
    ) {
        CourseChangesApplicationStatus courseChangesApplicationStatus1 = new CourseChangesApplicationStatus()
                .setCollegeId(collegeId).setName(name).setEnglishName(englishName);                                                 //合成CourseChangesApplicationStatus对象
        List<CourseChangesApplicationStatus> list1 = courseChangesApplicationStatusMapper.findAll(courseChangesApplicationStatus1); //获取List<CourseChangesApplicationStatus>
        List<CourseChangesApplicationStatusVO> list = new ArrayList<>();                                                            //初始化 List<CourseChangesApplicationStatusVO>
        for (CourseChangesApplicationStatus courseChangesApplicationStatus : list1) {                                                //循环遍历
            CourseChangesApplicationStatusVO courseChangesApplicationStatusVO = new CourseChangesApplicationStatusVO();             //初始化VO
            BeanUtils.copyProperties(courseChangesApplicationStatus, courseChangesApplicationStatusVO);                              //对象复制赋值
            String collegeName = collegeService.getCollegeName(courseChangesApplicationStatus.getCollegeId());                      //获取学校名称
            courseChangesApplicationStatusVO.setCollegeName(collegeName);                                                           //设置学校名称
            list.add(courseChangesApplicationStatusVO);                                                                             //加入到list
        }
        return ZQUResult.success(list);                                                                                             //成功返回
    }

    @ApiOperation(value = "获取课程变动申请类型字典信息", response = CourseChangesApplicationStatusVO.class)
    @RequestMapping(value = "/courseChangesApplicationStatus/{id}", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true)
    })
    public ZQUResult getById(@PathVariable BigInteger id) {
        CourseChangesApplicationStatus courseChangesApplicationStatus = courseChangesApplicationStatusMapper.selectById(id);//获取对象
        String collegeName = collegeService.getCollegeName(courseChangesApplicationStatus.getCollegeId());                  //获取学校名称
        CourseChangesApplicationStatusVO courseChangesApplicationStatusVO = new CourseChangesApplicationStatusVO();         //初始化VO
        BeanUtils.copyProperties(courseChangesApplicationStatus, courseChangesApplicationStatusVO);                          //对象复制赋值
        courseChangesApplicationStatusVO.setCollegeName(collegeName);                                                       //设置学校名称
        return new ZQUResult(HttpStatus.SUCCESS, "请求成功", courseChangesApplicationStatusVO);                        //返回对象数据
    }

    @ApiOperation(value = "添加")
    @RequestMapping(value = "/courseChangesApplicationStatus", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult add(BigInteger collegeId,
                         String name,
                         String englishName,
                         BigInteger byWho) {
        CourseChangesApplicationStatus courseChangesApplicationStatus = new CourseChangesApplicationStatus();//初始化
        courseChangesApplicationStatus.setCollegeId(collegeId);                                              //设置学校id
        courseChangesApplicationStatus.setName(name);                                                        //设置中文名称
        courseChangesApplicationStatus.setEnglishName(englishName);                                          //设置英文名称
        courseChangesApplicationStatus.setByWho(byWho);                                                      //设置操作者
        courseChangesApplicationStatusMapper.insert(courseChangesApplicationStatus);                         //执行插入
        return ZQUResult.success();                                                                          //操作成功返回
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/courseChangesApplicationStatus/{id}", method = RequestMethod.PUT)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collegeId", value = "学校id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = true),
            @ApiImplicitParam(name = "englishName", value = "英文名", paramType = "query", required = false),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult update(BigInteger collegeId,
                            @PathVariable BigInteger id,
                            String name,
                            String englishName,
                            BigInteger byWho) {
        CourseChangesApplicationStatus courseChangesApplicationStatus = courseChangesApplicationStatusMapper.selectById(id);//查出来
        courseChangesApplicationStatus.setByWho(byWho);                                                      //修改操作者
        courseChangesApplicationStatusMapper.updateById(courseChangesApplicationStatus);                     //更新操作者
        courseChangesApplicationStatusMapper.deleteById(id);                                                 //逻辑删除
        courseChangesApplicationStatus = new CourseChangesApplicationStatus();                               //初始化数据模型
        courseChangesApplicationStatus.setCollegeId(collegeId);                                              //设置学校id
        courseChangesApplicationStatus.setName(name);                                                        //设置中文名称
        courseChangesApplicationStatus.setEnglishName(englishName);                                          //设置英文名称
        courseChangesApplicationStatus.setByWho(byWho);                                                      //设置操作者
        courseChangesApplicationStatus.setLastVersion(id);                                                   //设置上一个版本号
        courseChangesApplicationStatusMapper.insert(courseChangesApplicationStatus);                         //执行插入
        return ZQUResult.success();                                                                          //操作成功返回
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/courseChangesApplicationStatus/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", required = true),
            @ApiImplicitParam(name = "byWho", value = "用户id", paramType = "query", required = true)
    })
    public ZQUResult delete(@PathVariable BigInteger id,
                            BigInteger byWho) {
        CourseChangesApplicationStatus courseChangesApplicationStatus = courseChangesApplicationStatusMapper.selectById(id);//查出来
        courseChangesApplicationStatus.setByWho(byWho);                                                      //修改操作者
        courseChangesApplicationStatusMapper.updateById(courseChangesApplicationStatus);                     //更新操作者
        courseChangesApplicationStatusMapper.deleteById(id);                                                 //逻辑删除
        return ZQUResult.success();                                                                          //操作成功返回
    }
}

